package com.yhao.stock.common.utils;

import com.yhao.stock.common.dto.UndulateModel;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author : yhao
 * @Date : 2023/11/22 - 9:09
 * @Desc : TODO
 */
public class StockUtil {

    public static void main(String[] args) {
        String filePath = "D:\\a.txt";

        List<String> contents = FileUtil.readTxt(filePath);
        ArrayList<UndulateModel> undulateModels = new ArrayList<>();
        Integer continuous = 5;
        Integer deviation = 20;
        UndulateModel undulateModel = null;
        Integer indexValue = 0;
        // for (int i = 0; i < contents.size(); i++) {
        //     if (i == 0) {
        //         undulateModel = findPeak(2, contents, continuous, deviation);
        //     } else {
        //         contents = contents.subList(indexValue + 1, contents.size());
        //
        //         undulateModel = findPeak(undulateModel.getFlag(), contents, continuous, deviation);
        //         if (undulateModel.isEnd() == true) {
        //             break;
        //         }
        //     }
        //     // TODO 这段逻辑还需要冲洗考虑下
        //     Integer index = undulateModel.getIndex();
        //     undulateModel.setIndex(indexValue + index);
        //     indexValue = indexValue + index;
        //     undulateModels.add(undulateModel);
        // }

        for (int i = 0; i < contents.size(); i++) {
            undulateModel = findPeak(2, contents, continuous, deviation);
            undulateModels.add(undulateModel);
        }
        for (UndulateModel model : undulateModels) {
            System.out.println("model===========>>>" + model);
        }
    }

    /**
     * @param flag       查找类别
     * @param contents   查找数组
     * @param continuous 连续值（前后比较 几个值的极值）
     * @param deviation  < 偏差度。以波峰为例: （最大值-当前值）/ (最大值-均值) * 10 >
     * @return
     * @desc 查找极值
     */
    public static UndulateModel findPeak(int flag, List<String> contents, Integer continuous, Integer deviation) {
        UndulateModel undulateModel = null;
        BigDecimal totalAvgPrice = totalAvgPrice(contents); // 全部平均价格
        BigDecimal totalMaxPrice = new BigDecimal(Collections.max(contents)); // 全部的最大值
        BigDecimal totalMinPrice = new BigDecimal(Collections.min(contents)); // 全部的最小值
        BigDecimal middleDiffPrice = totalMaxPrice.subtract(totalAvgPrice).setScale(2, RoundingMode.HALF_UP);
        for (int i = continuous; i < contents.size() - continuous; i++) {
            BigDecimal currPrice = new BigDecimal(contents.get(i));
            List<String> beforeFiveList = contents.subList(i - continuous, i);
            List<String> afterFiveList = contents.subList(i + 1, i + continuous + 1);
            // 获取波峰
            // if (flag == 2 || flag == 0) {
            if (currPrice.compareTo(totalAvgPrice(beforeFiveList)) > 0 &&
                    currPrice.compareTo(totalAvgPrice(afterFiveList)) > 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.max(beforeFiveList))) > 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.max(afterFiveList))) > 0) {
                if (currPrice.compareTo(totalAvgPrice) > 0) {
                    if (totalMaxPrice.subtract(currPrice).divide(middleDiffPrice, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(10)).compareTo(new BigDecimal(deviation)) < 0) {
                        // System.out.println("i  ==" + i
                        //         + " currPrice==" + currPrice
                        //         + " bm==" + Collections.max(beforeFiveList)
                        //         + " am==" + Collections.max(afterFiveList)
                        //         + " ba==" + totalAvgPrice(beforeFiveList)
                        //         + " aa==" + totalAvgPrice(afterFiveList));
                        return new UndulateModel(i, currPrice, 1, false);
                    }
                }
            }
            // }
            // 获取波谷
            // if (flag == 2 || flag == 1) {
            if (currPrice.compareTo(new BigDecimal(Collections.min(beforeFiveList))) < 0 &&
                    currPrice.compareTo(new BigDecimal(Collections.min(afterFiveList))) < 0 &&
                    currPrice.compareTo(totalAvgPrice(beforeFiveList)) < 0 &&
                    currPrice.compareTo(totalAvgPrice(afterFiveList)) < 0) {
                if (currPrice.compareTo(totalAvgPrice) < 0) {
                    if (currPrice.subtract(totalMinPrice).divide(middleDiffPrice, 2, RoundingMode.HALF_UP).multiply(new BigDecimal(10)).compareTo(new BigDecimal(deviation)) < 0) {
                        // System.out.println("i==" + i
                        //         + " currPrice==" + currPrice
                        //         + " bm==" + Collections.min(beforeFiveList)
                        //         + " am==" + Collections.min(afterFiveList)
                        //         + " ba==" + totalAvgPrice(beforeFiveList)
                        //         + " aa== " + totalAvgPrice(afterFiveList));
                        return new UndulateModel(i, currPrice, 0, false);
                    }
                }
            }
            // }
        }
        return new UndulateModel(0, new BigDecimal(0), 2, true);
    }

    public static BigDecimal totalAvgPrice(List<String> contents) {
        BigDecimal totalPrice = new BigDecimal(0);
        BigDecimal count = new BigDecimal(contents.size());
        for (String content : contents) {
            totalPrice = totalPrice.add(new BigDecimal(content));
        }
        return totalPrice.divide(count, 2, RoundingMode.HALF_UP);
    }

}
